﻿using UnityEngine;
using System.Collections;

public class PlayerControl : MonoBehaviour {

	public float speed = 1.5f;
	public float jumpHeight;
	private bool facingRight = true;

	public bool grounded = false;
	public Transform groundCheck;
	float groundRadius = 0.2f;
	public LayerMask whatIsGround;

	public int life;

	Animator anim;

	void Start () {
		anim = GetComponent<Animator>();
	}

	void FixedUpdate () {

		grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround); 
		anim.SetBool ("Ground", grounded);

		float move = Input.GetAxis ("Horizontal");

		anim.SetFloat ("Speed", Mathf.Abs (move));

		// anim.SetFloat ("VSpeed", gameObject.GetComponent<Rigidbody2D> ().velocity.y); Fall / Sprunggeschwindigkeit Animation

		if (move > 0 && !facingRight)
			Flip ();
		else if (move < 0 && facingRight)
			Flip ();

		//GetComponent<Rigidbody2D>().velocity = new Vector2(move*maxSpeed, GetComponent<Rigidbody2D>().velocity.y); Andere Möglichkeit für Bewegung, Kein KeyCode
	}

	void Update ()
	{
		if (Input.GetKey (KeyCode.A))  {
			transform.position += Vector3.left * speed * Time.deltaTime;
		}
		if (Input.GetKey (KeyCode.D)) {
			transform.position += Vector3.right * speed * Time.deltaTime;
		}

		if (Input.GetKeyDown (KeyCode.Space) && grounded == true) {
			gameObject.GetComponent<Rigidbody2D>().AddForce(Vector2.up * jumpHeight);
			anim.SetTrigger ("Boden");
		}
	}


	public void Flip() {
		facingRight = !facingRight;
		Vector3 theScale = transform.localScale;
		theScale.x *= -1;
		transform.localScale = theScale;
	}
}
